﻿

using UnityEngine;

using System.Collections;
using System.Collections.Generic;
public class MapUnLimited : MonoBehaviour
{


    // Use this for initialization
    void Start()
    {


    }
    //地图队列
    Queue<Transform> CubeQueue;
    //地图预制件，当然你也可以选取多个不同预制件的数组，以便地图多样化
    public Transform cubeProfab;
    //public Transform[] cubeProfab;
    //是否初始化地图
    bool isInitialCube = false;
    //初始化地图数目
    int InitialCubeNum = 50;
    //地图初始化z坐标，选择适合的举例
    public float InitialDistance = 0;
    //地图衔接距离
    public float CubeDistance = 1;
    //主角
    public Transform Runner;
    // Update is called once per frame
    void Update()
    {
        if (!isInitialCube)
        {
            CubeQueue = new Queue<Transform>();
            for (int i = 0; i < InitialCubeNum; i++)
            {
                Transform cube = (Transform)Transform.Instantiate(cubeProfab, new Vector3(Runner.position.x, 0, Runner.position.z + i * CubeDistance), Quaternion.identity);
                CubeQueue.Enqueue(cube);
            }
            isInitialCube = true;
        }
       Transform t = CubeQueue.Peek();
        if (t!= null)
        {
            if (Runner.position.z - t.position.z >= 2f)
            {
                Transform cube = CubeQueue.Dequeue();
                cube.position = new Vector3(Runner.position.x, 0, CubeQueue.ToArray()[CubeQueue.Count - 1].position.z +  CubeDistance);
                CubeQueue.Enqueue(cube);
            }
        }
       
    }
}